package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.dao.HospitalMapper;
import com.zhentao.pojo.Doctor;
import com.zhentao.dao.DoctorMapper;
import com.zhentao.pojo.Hospital;
import com.zhentao.pojo.User;
import com.zhentao.pojo.perms.Pages;
import com.zhentao.service.IDoctorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mp
 * @since 2025-03-26
 */
@Service
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements IDoctorService {
@Autowired
DoctorMapper doctorMapper;
@Autowired
    HospitalMapper hospitalMapper;

    @Override
    public Result list(Pages pages) {
        Page<Doctor> objectPage = new Page<>(pages.getPageNum(), pages.getPageSize());
        QueryWrapper<Doctor> objectQueryWrapper = new QueryWrapper<>();
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(!user.getRName().contains("管理员")){
            objectQueryWrapper.eq("create_by",user.getUsername());
        }
        objectQueryWrapper.like(!StringUtils.isEmpty(pages.getDoctorName()),"doctor_name",pages.getDoctorName());
        doctorMapper.selectPage(objectPage,objectQueryWrapper);
        List<Doctor> records = objectPage.getRecords();
        for (Doctor record : records) {
            Integer hospitalId = record.getHospitalId();
            Hospital hospital = hospitalMapper.selectById(hospitalId);
            if(hospital!=null){
                record.setHospitalName(hospital.getHospitalName());
            }
        }
        return objectPage!=null?Result.OK(objectPage):Result.ERROR();
    }

//    @Override
//    public Result list(Pages pages) {
//        Page<Doctor> objectPage = new Page<>(pages.getPageNum(), pages.getPageSize());
//        QueryWrapper<Doctor> objectQueryWrapper = new QueryWrapper<>();
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        User user = (User) authentication.getPrincipal();
//        if(!user.getRName().contains("管理员")){
//            objectQueryWrapper.eq("create_by",user.getUsername());
//        }
//        objectQueryWrapper.like(!StringUtils.isEmpty(pages.getDoctorName()),"doctor_name",user.getRName());
//        doctorMapper.selectPage(objectPage,objectQueryWrapper);
//        List<Doctor> records = objectPage.getRecords();
//        for (Doctor record : records) {
//            Integer hospitalId = record.getHospitalId();
//            Hospital hospital = hospitalMapper.selectById(hospitalId);
//            if(hospital!=null){
//               record.setHospitalName(hospital.getHospitalName());
//            }
//        }
//        return objectPage!=null?Result.OK(objectPage):Result.ERROR();
//    }
//    @Override
//    public Result list(Pages pages) {
//        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        Page<Doctor> objectPage = new Page<>(pages.getPageNum(), pages.getPageSize());
//        QueryWrapper<Doctor> objectQueryWrapper = new QueryWrapper<>();
//        if(!user.getRName().contains("管理员")){
//        objectQueryWrapper.eq("create_by",user.getUsername());
//        }
//        objectQueryWrapper.like(!StringUtils.isEmpty(pages.getDoctorName()),"doctor_name",pages.getDoctorName());
//        doctorMapper.selectPage(objectPage,objectQueryWrapper);
//        List<Doctor> records = objectPage.getRecords();
//        for (Doctor record : records) {
//            Integer id = record.getHospitalId();
//            Hospital hospital = hospitalMapper.selectById(id);
//            if(hospital!=null){
//                record.setHospitalName(hospital.getHospitalName());
//            }
//        }
//        return objectPage!=null?Result.OK(objectPage):Result.ERROR();
//        Page<Doctor> objectPage = new Page<>(pages.getPageNum(),pages.getPageSize());
//       QueryWrapper<Doctor> objectQueryWrapper = new QueryWrapper<>();
//       objectQueryWrapper.like(!StringUtils.isEmpty(pages.getDoctorName()),"doctor_name",pages.getDoctorName());
//       doctorMapper.selectPage(objectPage,objectQueryWrapper);
//      List<Doctor> records = objectPage.getRecords();
//      for (Doctor record : records) {
//            Integer doctorId = record.getHospitalId();
//          Hospital hospital = hospitalMapper.selectById(doctorId);
//          if(hospital!=null){
//               record.setHospitalName(hospital.getHospitalName());
//         }
//      }
//    return objectPage!=null?Result.OK(objectPage):Result.ERROR();
//}
}
